package com.feicheng.lanniao.controller;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;



import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feicheng.lanniao.entity.Car;


import com.feicheng.lanniao.entity.CarRentalClientVo;
import com.feicheng.lanniao.entity.CarRentalInfo;
import com.feicheng.lanniao.entity.CarRentalOwner;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.result.ResultInfo;


import com.feicheng.lanniao.service.ICarRentalClientVoService;
import com.feicheng.lanniao.service.ICarRentalInfoService;
import com.feicheng.lanniao.service.ICarRentalOwnerService;
import com.feicheng.lanniao.service.ICarService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/lanniao-admin/car-rental-info")
public class CarRentalClientVoController {
	@Autowired
	ICarRentalClientVoService ICarRentalClientVoService;
	/**
	 * 查询所有租车信息 并进行分页
	 * @param clientId 用户ID
	 * @param searchKey 模糊查询参数
	 * @param conditionId 车龄ID
	 * @param mileageId 车辆里程ID
	 * @param brandId 品牌ID
	 * @param rentalPriceId 出租价格ID
	 * @param type 车辆类型
	 * @param ownerShip 车牌归属地
	 * @param currentPage 当前页
	 * @param pageSize 每页分多少条
	 * @return 租车集合
	 */
	@GetMapping("/page")
	public ResultInfo rentCarList(Integer clientId,
								String searchKey,
								Integer conditionId, 
								Integer mileageId, 
								Integer brandId,  
								Integer rentalPriceId,
								String type,
								String ownerShip,
								@RequestParam(required = false,defaultValue = "1") Integer currentPage, 
								@RequestParam(required = false,defaultValue = "10") Integer pageSize) {
		
		return ICarRentalClientVoService.rentCarList(clientId,searchKey,conditionId, mileageId, brandId,rentalPriceId,type,ownerShip, currentPage, pageSize);
	}
	/**
	 * 查询1条租车信息
	 * @param carId carID
	 * @return 租车对象
	 */
	@GetMapping("/info")
	public ResultInfo getOneCr(Integer carId) {
		return ICarRentalClientVoService.getOneRentalBuyInfo(carId);
	}
	/**
	 * 修改租车信息
	 * @param carRentalInfo 对象
	 * @return boolean值
	 */
	@PutMapping("/info")
	public ResultInfo editCr(CarRentalClientVo carRentalInfo) {
		System.out.println(carRentalInfo);
		return ICarRentalClientVoService.editOneCarRentalInfo(carRentalInfo);
	}
	/**
	 * 删除租车信息
	 * @param carRentalInfoId 根据ID删除租车信息
	 * @return boolean值
	 */
	@DeleteMapping("/info")
	public ResultInfo deleteCar(Integer carRentalInfoId) {
		return ICarRentalClientVoService.delOneCar(carRentalInfoId);	
	}
	@Autowired
	private ICarRentalInfoService icarRentalInfoService;
	@Autowired
	private ICarService iCarService;
	@Autowired
	private ICarRentalOwnerService carRentalOwnerService;
	/**
	 * 增加租车信息
	 * @param name 车名
	 * @param carCondition 车龄
	 * @param mileage 车辆表显里程
	 * @param displacement 汽车排量
	 * @param transmissionCase 变速箱
	 * @param ownerShip 车牌归属地
	 * @param color 车辆颜色
	 * @param productionDate 生产日期
	 * @param brandId 品牌ID
	 * @param type 车型
	 * @param standard 排放标准
	 * @param seats 座数
	 * @param energy 燃油类型
	 * @param drive 驱动
	 * @param imgUrl 图片地址
	 * @param sellPrice 卖车价格
	 * @param rentalPrice 租车价格
	 * @param country 产地
	 * @param ct 创建时间
	 * @param clientId 用户ID
	 * @return boolean值
	 */
	@PostMapping("/info")
	@Transactional
	public ResultInfo addResultInfo(
			Integer carCondition,  // not null
			Integer mileage,     // not null
			String ownerShip,
			Integer brandId, // not null
			String type,      //not null
			String imgUrl,
			Integer rentalPrice,
			Integer clientId,  // not null
			String tel,
			String wxNumber
			) {
		Car car = new Car();
		car.setCarCondition(carCondition);
		car.setMileage(mileage);
		car.setOwnerShip(ownerShip);
		car.setBrandId(brandId);
		car.setType(type);
		car.setImgUrl(imgUrl);
		car.setRentalPrice(rentalPrice);
		iCarService.save(car);

		//创建CarRentalInfo对象
		CarRentalInfo carRentalInfo = new CarRentalInfo();
		carRentalInfo.setCarId(car.getCarId());
		carRentalInfo.setRentalPrice(car.getRentalPrice());
		carRentalInfo.setTel(tel);
		carRentalInfo.setWxNumber(wxNumber);
		System.out.println();
		icarRentalInfoService.save(carRentalInfo);
		//创建中间表对象
		CarRentalOwner carRentalOwner = new CarRentalOwner();
		if(clientId == null){
			throw new SystemException("ClientId不能为空");
		}
		carRentalOwner.setClientId(clientId);
		carRentalOwner.setCarRentalInfoId(carRentalInfo.getCarRentalInfoId());
		System.out.println(carRentalInfo.getCarRentalInfoId());
		carRentalOwnerService.save(carRentalOwner);
		return ResultInfo.success();
	}
}